package com.hao.hospital.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.joda.time.tz.UTCProvider;
import org.springframework.web.multipart.MultipartFile;

import com.hao.hospital.model.HospitalPerson;
import com.hao.hospital.model.Person;
import com.hao.hospital.service.MedicalService;
import com.hao.hospital.service.PaymentService;
import com.hao.hospital.service.PersonService;
import com.hao.hospital.service.UserService;
import com.hao.hospital.util.QiniuUtil;
import com.hao.hospital.util.TimeUtil;
import com.hao.hospital.util.UploadingImageUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class BackstageAction extends Controller {
	// 登录页面
	public void login() {
		render("login.html");
	}

	public void work() {
		render("work.html");
	}
	public void chart(){
		render("chart.html");
	}
	/**
	 * 患者信息分页
	 */
	public void userPage() {
		int pageNum = getParaToInt("pageNum");
		int person_id = getSessionAttr("person_id");
		String condition = getSessionAttr("condition");
		System.out.println("数据来自seesion:" + condition);
		setAttr("infoPage", MedicalService.me().getInfoByPersonId(person_id, pageNum, condition));
		render("list.html");
	}

	// 登录信息验证页面
	public void index() {
		String uname = getPara("name");
		String upass = getPara("password");

		if (PersonService.me().checkLogin(uname, upass) == true) {
			System.out.println(PersonService.me().checkLogin(uname, upass));
			Record record = PersonService.me().doctorinfo_getBylogin(uname, upass);
			List<Record> list = PersonService.me().getPowerList(record.getInt("person_id"));
			setAttr("list", list);
			setSessionAttr("person_id", record.getInt("person_id"));
			setSessionAttr("uname", uname);
			setAttr("person_name", record.get("person_name"));
			// 权限对应的页面
			render("index.html");
			return;
		} else {
			setAttr("name", uname);
			setAttr("flag", "1");
			render("login.html");
		}

	}

	/**
	 * 修改密码
	 */
	public void changepass() {
		String mpass = getPara("mpass");
		String newpass = getPara("newpass");
		String uname = getSessionAttr("uname");
		if (PersonService.me().checkLogin(uname, mpass) == true) {
			if (PersonService.me().changePass(uname, newpass)) {
				setAttr("rtnmsg", "修改密码成功！");
				setAttr("uname", uname);
				render("pass.html");
			}
		} else {
			setAttr("rtnmsg", "原密码错误");
			render("pass.html");
		}
	}

	/**
	 * 所有患者预约记录
	 */
	public void UserRegister() {
		int pageNumber = getParaToInt("pageNumber");
		setAttr("infoPage", MedicalService.me().getAllUserRegister(pageNumber));
		render("register.html");
	}
	public void updatePerson(){
		HospitalPerson hp =getModel(HospitalPerson.class);
		hp.update();
		int person_id=getSessionAttr("person_id");
		setAttr("person", PersonService.me().doctorinfo(person_id));
		setAttr("flag", "true");
		render("updateperson.html");
	}
	
	/**
	 * 存医生信息
	 */
	public void addPerson() {
		HospitalPerson hospitalPerson = new HospitalPerson();
		 try {
			String folderName = UUID.randomUUID().toString(); 
			UploadFile uf=getFile(getPara("imgUrl"),folderName);
			hospitalPerson = getModel(HospitalPerson.class);
			hospitalPerson.setHeadURL("/upload/temp/"+folderName+"/"+getFile("imgUrl").getOriginalFileName());
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
//		System.out.println(uf.getUploadPath());
//		System.out.println(uf.getFileName());
		Person person = getModel(Person.class);
		
		try {
			hospitalPerson.save();
			int person_id = hospitalPerson.getInt("person_id");
			person.setPersonId(person_id);
			person.save();
			redirect("/admin/indexBackPage?param=13");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void settime() throws ParseException {
		String time = getPara("time");
		if (StringUtils.isEmpty(time)) {
			setAttr("flag", "false");
			render("work.html");
			return;
		}
		String[] times = time.split(",");
		List<Long> list = new ArrayList<>();
		int person_id = getSessionAttr("person_id");
		for (String t : times) {
			String x = t.substring(0, 4) + "-" + t.substring(5, 7) + "-" + t.substring(8, 10);
			PersonService.me().worktime(person_id, TimeUtil.me().dateToStamp(x));
		}
		setAttr("add", "true");
		render("work.html");
	}

	/**
	 * 全部跳转页面要过一层controller 转换
	 */
	public void indexBackPage() {
		int param = getParaToInt("param");
		String condition = getPara("condition");
		Integer pageNum = getParaToInt("pageNum");
		System.out.println(param + "参数");
		int person_id = getSessionAttr("person_id");
		switch (param) {
		case 1:
			setAttr("infoPage", MedicalService.me().getAllUserRegister(1));
			render("register.html");
			break;
		case 2:
			setAttr("uname", getSessionAttr("uname"));
			render("pass.html");
			break;
		case 3:
			render("page.html");
			break;
		case 4:
			List<Record> list = PaymentService.me().getTodayRegistertInfobyPersonId(person_id);
			setAttr("objectlist", list);
			render("adv.html");
			break;
		case 5:
			render("worktime.html");
			break;
		case 6:

			// 获取月初和月末的时间戳，判断是否有预约，有就不能再设置

			render("work.html");
			break;
		case 7:// 病历管理

			if (StringUtils.isNotEmpty("condition")) {
				setSessionAttr("condition", condition);
			}
			System.out.println("参数为？" + condition);
			// 空白查询有问题
			setAttr("infoPage", MedicalService.me().getInfoByPersonId(person_id, 1, condition));
			render("list.html");
			break;
		case 8:
			render("add.html");
			break;
		case 9:
			render("addperson.html");
			break;
		case 10:
			int user_id = getParaToInt("user_id");
			Record user = UserService.me().bill(person_id, user_id);
			String context = user.getStr("pathogeny");
			String[] contexts = context.split(" ");
			Map<String, String> data = new HashMap<>();
			int i = 1;
			for (String s : contexts) {
				data.put("param" + i, s);
				i++;
			}
			setAttr("user", user);
			setAttr("map", data);
			render("ohzi.html");
			break;
		case 11:
			if (pageNum == null || "".equals(pageNum)) {
				pageNum = 1;
			}
			setAttr("infoPage", UserService.me().allinfo(pageNum));
			render("pay.html");
			break;
		default:
			break;
		case 12:
			String user_id1 = getPara("user_id");
			if (pageNum == null || "".equals(pageNum)) {
				pageNum = 1;
			}
			Page<Record> page = PaymentService.me().getPayRecord(pageNum, condition);
			System.out.println(page);
			setAttr("infoPage", page);
			System.out.println("condition:" + condition);
			if (StringUtils.isEmpty(condition)) {
				System.out.println("首页进入");
				setAttr("condition", "");
			} else {
				System.out.println("进入到else");
				Record record = page.getList().get(0);
				if (record == null) {
					user_id1 = "-1";
				} else {
					user_id1 = record.getInt("user_id").toString();

				}
				System.out.println("user_id:" + user_id1);
				if ("支付".equals(condition) || "充值".equals(condition)) {
					user_id1 = null;
				}
				setAttr("condition", condition);
			}
			setAttr("count", PaymentService.me().countArray(user_id1));
			System.err.println("Ok");
			render("record.html");
			break;
		case 13:
			if (pageNum == null || "".equals(pageNum)) {
				pageNum = 1;
			}
			setAttr("infoPage", PersonService.me().allPersonInfo(pageNum, condition));
			render("person.html");
			break;
		case 14:
			//int person_id=getSessionAttr("person_id");
			setAttr("person", PersonService.me().doctorinfo(person_id));
			render("updateperson.html");
			break;
		}
	
	}

	/**
	 * 医生分页
	 */
	public void personInfo() {
		int pageNumber = getParaToInt("pageNum");
		setAttr("infoPage", PersonService.me().allPersonInfo(pageNumber));
		render("person.html");
		// renderJson(PersonService.me().allPersonInfo(pageNumber));
	}

	public void coutArray() {
		int pageNumber = getParaToInt("pageNum");
		String user_id1 = getPara("user_id");
		setAttr("infoPage", PaymentService.me().getPayRecord(pageNumber));
		setAttr("count", PaymentService.me().countArray(user_id1));
		render("record.html");
	}
}
